<?php
namespace app\admin\controller;
use think\Controller;
use think\Controllers;

use think\Session;
use think\Cookie;
use think\Jump;
use think\Db;
class Partner extends Controllers{

	public function index(){
		$sql ="select m.nickname,m.avatar,m.uid from ims_mc_members as m  where m.power = 1 group by m.uid";
		$arr = DB::query($sql);
		foreach ($arr as $key => $value) {
			$sql = "select count(t.id) as team_count from ims_xuan_zy_commission_team as t left join ims_mc_members as m on t.uid = m.uid  where t.partner_id = {$value['uid']}  and m.power = 2";
			$arr[$key]['team_count'] = DB::query($sql)[0]['team_count'];
		}
   	    $this->assign("list",$arr);
   	   return $this->fetch("admin/partner");


	}
	public function add(){

	$pindex    = max(1, intval($_REQUEST['page']));
	$psize     = 20;

	$wheres = " AND a.uniacid= 2 and a.power <> 1";

	if(!empty($_REQUEST['id'])){

		$wheres.=" and a.uid= '{$_REQUEST['id']}'  ";
		$this->assign("id",$_REQUEST['id']);

		
	}
    if(!empty($_REQUEST['phone'])){

        $wheres.=" and a.mobile='{$_REQUEST['phone']}' ";
		$this->assign("phone",$_REQUEST['phone']);



    }
    if(!empty($_REQUEST['yewuy'])){

        $wheres.=" and b.yewuy= '{$_REQUEST['yewuy']}' ";
		$this->assign("yewuy",$_REQUEST['yewuy']);


    }

	if(!empty($_REQUEST['nickname'])){

		$wheres.=" and a.nickname LIKE '%".$_REQUEST['nickname']."%'";
		$this->assign("nickname",$_REQUEST['nickname']);
		
	}
	$wheres .= " and b.idcard <> ''";
    if(!empty($_REQUEST['level'])&&$_REQUEST['level']!=0){

        $wheres2=" and b.level=".intval($_REQUEST['level']);
        	$this->assign("level",$_REQUEST['level']);
		

        $sql ="SELECT a.uid,a.nickname,b.yewuy,a.mobile as phone,a.avatar,a.credit2,a.createtime,b.level,b.name,b.weixin,b.mobile,b.idcard,a.power FROM ims_mc_members as a inner join  ims_xuan_zy_person as b on b.uid=a.uid {$wheres2} WHERE 1 {$wheres} order by a.uid desc";
        $sql2 ="SELECT count(*) as count FROM ims_mc_members as a inner join ims_xuan_zy_person as b on b.uid=a.uid {$wheres2} WHERE 1 {$wheres} order by a.uid desc";

    }else{

        $sql ="SELECT a.uid,a.nickname,b.yewuy,a.avatar,a.mobile as phone,a.credit2,a.createtime,b.level,b.name,b.weixin,b.mobile,b.idcard,a.power  FROM ims_mc_members as a left join ims_xuan_zy_person as b on b.uid=a.uid   WHERE 1 {$wheres} order by a.uid desc";
        $sql2 ="SELECT count(*) as count FROM  ims_mc_members as  a left join ims_xuan_zy_person as b on b.uid=a.uid WHERE 1 {$wheres} order by a.uid desc";

    }
    $limit = ($pindex-1)*$psize;
    $count = DB::query($sql2)[0]['count'];
    if(request()->isajax()){
	  $sql .= "  limit {$limit},{$psize}";
    $list = DB::query($sql);
        foreach ($list as $k => &$v) {
    	switch ($v['power']) {
    		case '1':
    			$v['power'] = "股东"; 
    			break;
			case '2':
    			$v['power'] = "团队"; 
    			break;
    		
    		default:
    			$v['power'] = "普通用户"; 
    			# code...
    			break;
    	}
    }
    	echo json_encode($list);die;
    }
    $this->assign("list",$list);
    $this->assign("count",$count);
	return $this->fetch("admin/par_member");

	

	}

	public function dl(){
		$id  = $_REQUEST['id'];
        $sql = "select id from ims_xuan_zy_commission_model where uid = 0 and `default` = 1 and type = 0";
        $model_id  = DB::query($sql)[0]['id'];

		$self =  $this->random(8);
		$user = DB::table("ims_xuan_zy_person")->where("uid",$id)->find();
		$data = [
			"username"=>$user['name'],
			"password"=>md5("123456".$self),
			"salt"=>$self,
			"power"=>"1",
			"joindate"=>time(),
			"user_id"=>$id

		];
		DB::table("ims_users")->insert($data);
		DB::table("ims_mc_members")->where("uid","{$id}")->setField(["power"=>1]);
		DB::table("ims_xuan_zy_commission_team")->where("uid","{$id}")->setField(["com_model_id"=>$model_id]);
		DB::table("ims_xuan_zy_commission")->where("uid","{$id}")->setField(["power"=>1]);
		echo json_encode(true);

	}
	public function bank(){
		$id = $_REQUEST['id'];
		$sql ="select * from ims_xuan_zy_commission where uid = {$id}";
		$arr = DB::query($sql);
		$this->assign("list",$arr);
		$this->assign("uid",$id);
		return $this->fetch("admin/par_bank");

	}
	public function add_bank(){
		$id  =	$_REQUEST['id'];
		$uid  =	$_REQUEST['uid'];
		if($id){
			$sql = "select * from ims_xuan_zy_commission where id = {$id}";
			$info = DB::query($sql)[0];
			$this->assign("info",$info);
		}else{
			$sql = "select bankname from ims_xuan_zy_bank ";
			$bank = DB::query($sql);
			$this->assign("bank",$bank);
		}
		$this->assign("uid",$uid);
			return $this->fetch("admin/par_bankadd");


	}
	public function bank_upd(){
		$id = $_REQUEST['id'];
		$uid =$_REQUEST['uid'];
		if($id){
			$data['commision'] =  $_REQUEST['commision'];
			DB::table("ims_xuan_zy_commission")->where("id",$id)->setField($data);
			$this->success("修改成功","{$_SERVER['SCRIPT_NAME']}/admin/partner/bank?id={$uid}");			
		} else {
			$sql  = "select id from ims_xuan_zy_commission where uid = {$uid} and bank_name = '{$_REQUEST['bank_name']}'";
			$bank = DB::query($sql);
			$data['commision'] =  $_REQUEST['commision'];
			if($bank){
			DB::table("ims_xuan_zy_commission")->where("id",$id)->setField($data);
			$this->success("修改成功","{$_SERVER['SCRIPT_NAME']}/admin/partner/bank?id={$uid}");
			} else {
			$data['bank_name'] =  $_REQUEST['bank_name'];
			$data['power']=1;
			$data['type']=3;
			$data['uid']=$uid;
			$a = DB::table("ims_xuan_zy_commission")->insert($data);	
			}
			$this->success("添加成功","{$_SERVER['SCRIPT_NAME']}/admin/partner/bank?id={$uid}");

		}
	}


}